﻿using System;
using System.Collections.Generic;
using System.Linq;
using MedicWeb.MedicService;
using Microsoft.Reporting.WebForms;

namespace MedicWeb.forms.Reports.Statistic
{
    public partial class StatisticByDate : SessionPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var StartDate = new DateTime(2007, 03, 01);
            var EndDate = new DateTime(2011, 03, 01);
            var ms = new MainService();

            //Response.ContentType = "application/pdf";

            var ReportViewer1 = new ReportViewer();

            var ordersList = ms.GetOrderListFull(GetSession(), false).ToArray();
            //var ordersList = ms.GetOrderList(GetSession());
            //var ids = ordersList.Select(o => o.Id).ToArray();

            int[] ids = null;
            int[] spec = null;
            int[] inst = null;
            bool excel = false;
            string datefrom = string.Empty;
            string datetill = string.Empty;
            
            if (!Session["roi"].Equals(null))
            {
                ids = (int[])Session["roi"];
                Session.Remove("roi");
            }

            if (!Session["rsi"].Equals(null))
            {
                spec = (int[])Session["rsi"];
                Session.Remove("rsi");
            }

            if (!Session["rii"].Equals(null))
            {
                inst = (int[])Session["rii"];
                Session.Remove("rii");
            }

            if (!Session["excel"].Equals(null))
            {
                excel = (bool)Session["excel"];
                Session.Remove("excel");
            }

            if (!Session["datefrom"].Equals(null))
            {
                datefrom = (string)Session["datefrom"];
                Session.Remove("datefrom");
            }
            if (!Session["datetill"].Equals(null))
            {
                datetill = (string)Session["datetill"];
                Session.Remove("datetill");
            }

            

            //var tmp = ordersList.Where(o => ids.Contains(o.Id));
            //var tmp = ms.GetDoctorList(GetSession()).Where(d => ids.Contains(d.Order.Id));
            var statistics = ms.GetStatistic(GetSession(), ids);

            if (spec.Count()> 0)
            {

                var tmp = statistics.Where(st => spec.Contains(st.SpecialityId)).ToArray();
                statistics = tmp;
            }

            if (inst.Count() > 0)
            {

                var tmp = statistics.Where(st => inst.Contains(st.InstitutionId)).ToArray();
                statistics = tmp;
            }

            var sourceDoctors = new ReportDataSource("DataSet1", statistics);
            ReportViewer1.LocalReport.DataSources.Add(sourceDoctors);

            ReportViewer1.ProcessingMode = ProcessingMode.Local;
            ReportViewer1.LocalReport.ReportPath = @"forms\Reports\Statistic\Report1.rdlc";

            ReportViewer1.LocalReport.SetParameters(new ReportParameter("DateFrom", datefrom));
            ReportViewer1.LocalReport.SetParameters(new ReportParameter("DateTill", datetill));

            ReportViewer1.LocalReport.Refresh();
            if (!IsPostBack)
            {
            }
            if (excel)
            {

                Response.ContentType = "application/xls";
                // add content type header
                Response.AddHeader("Content-Type", "application/xls");
                // set the content disposition
                Response.AddHeader("Content-Disposition", "inline;filename=Report.xls");
                byte[] render = ReportViewer1.LocalReport.Render("Excel");
                Response.BinaryWrite(render);
            }
            else
            {
                Response.ContentType = "application/pdf";
                byte[] render = ReportViewer1.LocalReport.Render("PDF");
                Response.BinaryWrite(render);
            }
            
            Response.End();

            
            //GridView1.DataSource =  reportByDate;
            //GridView1.DataBind();
            

        }
    }
}